package com.hecate.common.db.basedao.provider.select;

import com.hecate.common.db.basedao.provider.HecateSqlHelper;
import org.apache.ibatis.mapping.MappedStatement;
import tk.mybatis.mapper.mapperhelper.MapperHelper;
import tk.mybatis.mapper.mapperhelper.MapperTemplate;

/**
 * @author 黄星星
 * @date 2020-12-18
 */
public class HecateBaseSelectProvider extends MapperTemplate {
    public HecateBaseSelectProvider(Class<?> mapperClass, MapperHelper mapperHelper) {
        super(mapperClass, mapperHelper);
    }

    /**
     * 查询
     *
     * @param ms
     * @return
     */
    public String selectOne(MappedStatement ms) {
        Class<?> entityClass = getEntityClass(ms);
        //修改返回值类型为实体类型
        setResultType(ms, entityClass);
        StringBuilder sql = new StringBuilder();
        sql.append(HecateSqlHelper.selectAllColumns(entityClass));
        sql.append(HecateSqlHelper.fromTable(entityClass, tableName(entityClass)));
        sql.append(HecateSqlHelper.whereAllIfColumns(entityClass, isNotEmpty()));
        return sql.toString();
    }

    /**
     * 查询
     *
     * @param ms
     * @return
     */
    public String select(MappedStatement ms) {
        Class<?> entityClass = getEntityClass(ms);
        //修改返回值类型为实体类型
        setResultType(ms, entityClass);
        StringBuilder sql = new StringBuilder();
        sql.append(HecateSqlHelper.selectAllColumns(entityClass));
        sql.append(HecateSqlHelper.fromTable(entityClass, tableName(entityClass)));
        sql.append(HecateSqlHelper.whereAllIfColumns(entityClass, isNotEmpty()));
        sql.append(HecateSqlHelper.orderByDefault(entityClass));
        return sql.toString();
    }

    /**
     * 查询
     *
     * @param ms
     * @return
     */
    public String selectByRowBounds(MappedStatement ms) {
        return select(ms);
    }

    /**
     * 根据主键进行查询
     *
     * @param ms
     */
    public String findByBid(MappedStatement ms) {
        final Class<?> entityClass = getEntityClass(ms);
        //将返回值修改为实体类型
        setResultType(ms, entityClass);
        StringBuilder sql = new StringBuilder();
        sql.append(HecateSqlHelper.selectAllColumns(entityClass));
        sql.append(HecateSqlHelper.fromTable(entityClass, tableName(entityClass)));
        sql.append(HecateSqlHelper.wherePKColumns(entityClass));
        return sql.toString();
    }

    /**
     * 查询总数
     *
     * @param ms
     * @return
     */
    public String selectCount(MappedStatement ms) {
        Class<?> entityClass = getEntityClass(ms);
        StringBuilder sql = new StringBuilder();
        sql.append(HecateSqlHelper.selectCount(entityClass));
        sql.append(HecateSqlHelper.fromTable(entityClass, tableName(entityClass)));
        sql.append(HecateSqlHelper.whereAllIfColumns(entityClass, isNotEmpty()));
        return sql.toString();
    }

    /**
     * 根据主键查询总数
     *
     * @param ms
     * @return
     */
    public String existsWithPrimaryKey(MappedStatement ms) {
        Class<?> entityClass = getEntityClass(ms);
        StringBuilder sql = new StringBuilder();
        sql.append(HecateSqlHelper.selectCountExists(entityClass));
        sql.append(HecateSqlHelper.fromTable(entityClass, tableName(entityClass)));
        sql.append(HecateSqlHelper.wherePKColumns(entityClass));
        return sql.toString();
    }

    /**
     * 查询全部结果
     *
     * @param ms
     * @return
     */
    public String findAll(MappedStatement ms) {
        final Class<?> entityClass = getEntityClass(ms);
        //修改返回值类型为实体类型
        setResultType(ms, entityClass);
        StringBuilder sql = new StringBuilder();
        sql.append(HecateSqlHelper.selectAllColumns(entityClass));
        sql.append(HecateSqlHelper.fromTable(entityClass, tableName(entityClass)));
        sql.append(" where ");
        sql.append(" cid = ").append("#{cid}");
        sql.append(" and status= 0");
        //sql.append(SqlHelper.orderByClause(entityClass));
        return sql.toString();
    }
}
